;(function($){
	$.fn.spa_menu_enterAccount = function(opts){
		var el = this;
		var opt = {
			bmh : "",
			ds : {},
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.setHtmlValue();
		this.initPlugin();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭模态
			el.find("#close-dialog,#modalBtnClose").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			});
			
			//确定
			el.find("#modalBtnCheck").unbind("click").click(function(){
				param.ds.sl = el.find("#sl").val();
				param.ds.remark = el.find("#remark").val();
				t.saveAccount();
			});
			
			//数量
			el.find("#sl_help").unbind("click").click(function(){
				el.modal("toggle");
				if(!$.isFunction($().digitalCalculator)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				$("#mykeyboard").digitalCalculator({
					number:el.find("#sl").val(),//数量
					status:status?status:true,
					title:$.i18n.propJsp("数量"),
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#sl").val(number)
						el.modal("toggle");
					},
					returnBack : function(){
						el.modal("toggle");
					}
				});
			});
			
			//选择查询技工
			el.find("#btnSearchSelJg").unbind("click").click(function(){
				
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){ 
					el.modal("toggle");
					$("#"+modalEle).modal("show");
			    	if(!$.isFunction($().spa_search_sel_jg)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.spa_search_sel_jg.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).spa_search_sel_jg({
						type : "search",
						bmh : param.bmh,
						dataBack:function(data){
							el.modal("toggle");
							t.setJgValue(data);
						},
						closeBack:function(){
							el.modal("toggle");
						}
					});
				})
			});
			
			//删除技工
			el.find("#btnDelJg").unbind("click").click(function(){
				t.setJgValue({});
			})
			
			//输入技工号查找
			el.find("#jgh").unbind("keydown").keydown(function(e){
				if(e.keyCode==13){
					var whereString = " and bh = '"+el.find("#jgh").val()+"' ";
					t.getJgDs(whereString,function(data){
						if(data.length>0){
							t.setJgValue(data[0]);
						}
					})
				}
			})
		},
		setJgValue : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
//			el.find("#jgbh").text(data.bh||"");
			el.find("#jgm").text(data.xm||"");
//			el.find("#jgh").text(data.bh||"");
			el.find("#jgh").val(data.bh||"");
			param.ds.jgbh = data.bh||"";
			param.ds.jgm = data.xm||"";
			param.ds.jgh = data.bh||"";
		},
		getJgDs : function(whereString,back){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			whereString = " and bmh = '"+param.bmh+"' " +whereString ;
			var arg1 = {"where":{"1":1},"fileds":["bh","groupby","xm","bmh","jg","pass","func","cp","depart","cp2","rights","cp_pre","cp_tc","cdepart"],
						"table":{"table":"viewJqGrid_ct_fwy"},"wherestring":["@@WS"," 1=1 "+whereString]}
			var arg2 = [{"page":1,"sord":"asc","rows":-1,"sidx":"groupby"}]
			jQuery.ajax({  
	    		type : 'POST',
	    		url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
	    		data : $.HSAPI_ASKJSONINFO(JSON.stringify(arg1),'RetrieveJqGridList',JSON.stringify(arg2),"list"),// CRS_ResvCheckModify
	    		dataType : 'json',  
	    		success : function(data){
//	    			console.log(data);
	    			if(data.RETURN&&data.RETURN!=1){
	    				if(data.hasOwnProperty("gritter")){
							if(data.hasOwnProperty("gritterstyle"))
								jf_successDelayed(data.gritter,2000,data.gritterstyle);
							else
								jf_successDelayed(data.gritter,2000); 
						}
	    				
	    			}else{
    					param.ds = data;
	    				back(data);
					}
	    		},  
	    		error : function(data){
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="modal-dialog" style="width:600px;">'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="modal-h4">'+$.i18n.propJsp("入账")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+
									
									'<div class="row">'+//widget-box 
										'<div class="col-sm-12">'+
										
											'<div class="profile-info-row">'+
												'<div id="label_rate" class="profile-info-name">'+$.i18n.propJsp("手牌号")+'</div>'+
												'<div class="profile-info-value">'+
													'<span id="th" class="accountInfo"></span>'+
												'</div>'+
											'</div>'+
											
											'<div class="profile-info-row">'+
												'<div id="label_rate" class="profile-info-name">'+$.i18n.propJsp("名称")+'</div>'+
												'<div class="profile-info-value">'+
													'<div class="col-sm-12 no-padding no-margin">'+
														'<span id="mc" class="accountInfo"></span>'+
														'<span id="bh" class="hide accountInfo"></span>'+
													'</div>'+
													'<div id="tcContainer" class="col-sm-12 no-padding no-margin" style="display:none;">'+
														'<div class="col-sm-6 no-padding no-margin">'+
															'<ul class="list-unstyled">'+
															'</ul>'+
														'</div>'+
														'<div class="col-sm-6 no-padding no-margin">'+
															'<ul class="list-unstyled">'+
															'</ul>'+
														'</div>'+
													'</div>'+
												'</div>'+
											'</div>'+
											
											'<div id="jgOption" class="profile-info-row">'+// style="display:none;"
												'<div id="label_rate" class="profile-info-name">'+$.i18n.propJsp("技工")+'</div>'+
												'<div class="profile-info-value">'+
													'<div class="col-sm-12 no-padding no-margin">'+
														'<input id="jgh" name="jgh" type="text" autocomplete="off"/>'+
//														'<span id="jgh" class="accountInfo hide"></span>'+
//														'<span id="jgbh" class="accountInfo hide"></span>'+
														'<span id="jgm" class="accountInfo"></span>'+
														'<div class="btn-group pull-right">'+
															'<button id="btnDelJg" class="btn btn-sm btn-info">'+
																$.i18n.propJsp('删除技工')+
															'</button>'+
															'<button id="btnSearchSelJg" class="btn btn-sm btn-info">'+
																$.i18n.propJsp('技工')+
															'</button>'+
														'</div>'+
													'</div>'+
												'</div>'+
											'</div>'+
										
											'<div class="profile-info-row">'+
												'<div id="label_rate" class="profile-info-name">'+$.i18n.propJsp("数量")+'</div>'+
												'<div class="profile-info-value">'+
													'<div class="input-group">'+
														'<input type="text" class="input-medium spinner accountInfo" id="sl" name="sl" style="font-size:15px;"/>'+
														'<span class="input-group-btn hide">'+
															'<button type="button" data-name="sl" class="btn btn-primary btn-sm" id="sl_help">'+
																'<i class="ace-icon fa fa-credit-card icon-on-right bigger-110"></i>'+
															'</button>'+
														'</span>'+
													'</div>'+
												'</div>'+
											'</div>'+
											
											'<div class="profile-info-row">'+
												'<div id="label_rate" class="profile-info-name">'+$.i18n.propJsp("备注")+'</div>'+
												'<div class="profile-info-value">'+
													'<textarea class="accountInfo" rows="4" cols="60" id="remark" name="remark"></textarea>'+
												'</div>'+
											'</div>'+
											
										'</div>'+
									'</div>'+
								
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="btn-group">'+
										'<button id="modalBtnCheck" type="button" class="btn btn-sm btn-info">'+
											'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("保存")+
										'</button>'+
									'</div>'+
									'<div class="btn-group">'+
										'<button id="modalBtnClose" type="button" class="btn btn-sm btn-default">'+
											'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+
										'</button>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		initPlugin : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.find("#sl").ace_spinner({
				value :1,
				min:-999999,
				max:999999,
				step:1, 
				on_sides: true, 
				icon_up:'ace-icon fa fa-plus smaller-75', 
				icon_down:'ace-icon fa fa-minus smaller-75', 
				btn_up_class:'btn-success' , 
				btn_down_class:'btn-danger',
			});
			
		},
		setHtmlValue : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.find(".accountInfo").each(function(k,v){
				$(v).text(param.ds[v.id]||"").val(param.ds[v.id]||"");
			});
			el.find("#th").text(param.ct_brzd.th);
			
			//套餐明细
			if(param.ds.tc_detail){
				var tc_detail = $.parseJSON(param.ds.tc_detail);
				var html1 = '';
				var html2 = '';
				$(tc_detail).each(function(k,v){
					if(k%2==0){
						html1 +='<li>'+
									'<i class="ace-icon fa fa-caret-right blue"></i>'+
									v.mc+'×'+v.dj +
								'</li>';
					}else{
						html2 +='<li>'+
									'<i class="ace-icon fa fa-caret-right blue"></i>'+
									v.mc+'×'+v.dj +
								'</li>';
					}
				});
				if(tc_detail.length>0){
					el.find("#tcContainer").show();
					el.find("#tcContainer").find("ul.list-unstyled").eq(0).html(html1);
					el.find("#tcContainer").find("ul.list-unstyled").eq(1).html(html2);
				}
			}else{
				el.find("#tcContainer").hide();
			}
			
			//技工
			if(param.ds.ct_cb_jg==1){
				el.find("#jgOption").show();
			}else{
				el.find("#jgOption").hide();
			}
		},
		saveAccount : function(){//保存入账
			var el = this.el;
			var param = this.param;
			var t = this;
			param.wait();
			var arg = {"where":{"bmh":param.bmh},
					   "ct_brzd":{"no":param.ct_brzd.no},
					   "ct_brxsxm":[param.ds],
					   "ct_brxsxm_tc":[]};
			if(param.ds.tc_detail){
				arg.ct_brxsxm_tc = $.parseJSON(param.ds.tc_detail);
				delete arg.ct_brxsxm[0].tc_detail;
			}
			jQuery.ajax({  
	    		type : 'POST',
//	    		async:false, 
	    		url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
	    		data : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'POSOrderMealAdd','{}'),// CRS_ResvCheckModify
	    		dataType : 'json',  
	    		success : function(data){
	    			param.goon();
	    			if(data.RETURN==1){
	    				if(data.hasOwnProperty("gritter")){
							if(data.hasOwnProperty("gritterstyle"))
								jf_successDelayed(data.gritter,2000,data.gritterstyle);
							else
								jf_successDelayed(data.gritter,2000); 
						}
	    				param.dataBack();
	    				el.modal("hide");
	    				el.empty();
	    			}else{
						if(data.hasOwnProperty("gritter")){
							if(data.hasOwnProperty("gritterstyle"))
								jf_successDelayed(data.gritter,2000,data.gritterstyle);
							else
								jf_successDelayed(data.gritter,2000); 
						}else{
		    				jf_alert(data.MSG)
						}
					}
	    		},  
	    		error : function(data){
	    			param.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
	}
	

	/*
	 * 私有函数
	 * 获取站点路径
	 */
	function getContextPath(){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		return path;
	}
})(jQuery);